package com.zheng.business.web.controller;

import com.zheng.business.dao.model.BusinessOrderUmbrellaConsumptionExample;
import com.zheng.business.dao.model.BusinessOrderUmbrellaTimecard;
import com.zheng.business.dao.model.BusinessOrderUmbrellaTimecardExample;
import com.zheng.business.rpc.api.BusinessOrderUmbrellaTimecardService;
import com.zheng.common.base.BaseController;
import com.zheng.common.constant.Constant;
import com.zheng.common.util.DateUtil;
import com.zheng.upms.rpc.api.UpmsApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manage/timecard")
@Api(value = "晴雨卡订单控制器", description = "晴雨卡订单控制器")
public class BusinessTimecardController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(BusinessTimecardController.class);

    @Autowired
    private BusinessOrderUmbrellaTimecardService businessOrderUmbrellaTimecardService;

    @Autowired
    private UpmsApiService upmsApiService;
    /**
     * 文章首页
     * @return
     */
    @ApiOperation(value = "晴雨卡订单首页")
    @RequiresPermissions("business:timecard:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/timecard/index.jsp";
    }

    @ApiOperation(value = "晴雨卡订单列表")
    @RequiresPermissions("business:timecard:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "orderno") String orderno,
            @RequestParam(required = false, defaultValue = "", value = "userid") Integer userid,
            @RequestParam(required = false, defaultValue = "", value = "payOrderId") String payOrderId,
            @RequestParam(required = false, defaultValue = "", value = "begintime") String begintime,
            @RequestParam(required = false, defaultValue = "", value = "endtime") String endtime,
            @RequestParam(required = false, defaultValue = "", value = "status") Integer status,
            @RequestParam(required = false, defaultValue = "", value = "cardtype") Integer cardtype,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        BusinessOrderUmbrellaTimecardExample example = new BusinessOrderUmbrellaTimecardExample();
        BusinessOrderUmbrellaTimecardExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(orderno)) {
            criteria.andOrdernoLike("%"+orderno+"%");
        }
        if (StringUtils.isNotBlank(payOrderId)) {
            criteria.andPayorderidEqualTo(payOrderId);
        }
        if (StringUtils.isNotBlank(begintime) && StringUtils.isNotBlank(endtime)) {
            criteria.andCreatetimeBetween(DateUtil.parseDate(begintime,DateUtil.FORMAT_YYYY_MM_DD),DateUtil.parseDate(endtime,DateUtil.FORMAT_YYYY_MM_DD));
        }

        if (userid!=null) {
            criteria.andUseridEqualTo(userid);
        }
        if (cardtype!=null && cardtype!=-1) {
            criteria.andCardtypeEqualTo(cardtype);
        }
        if (status!=null && status!=-1) {
            criteria.andStatusEqualTo(status);
        }
        //代理数据限制
        String userName = getLoginUserName();
        Integer agentId = upmsApiService.selectUserAgentIdByUsername(userName);
        if(agentId!=null && agentId!= Constant.CONST_SUPER_AGENTID){
            criteria.andAgentidEqualTo(agentId);
        }
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            example.setOrderByClause(sort + " " + order);
        }else{
            example.setOrderByClause(" CreateTime desc");
        }

        List<BusinessOrderUmbrellaTimecard> rows = businessOrderUmbrellaTimecardService.selectByExampleForOffsetPage(example, offset, limit);
        long total = businessOrderUmbrellaTimecardService.countByExample(example);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

}